/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * manage_inventory.java
 *
 * Created on Jul 25, 2011, 1:38:00 AM
 */
package introse1;


import java.util.ArrayList;
//import com.mysql.jdbc.Statement;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import javax.swing.table.TableModel.*;
import javax.swing.JTable.*;
/**
 *
 * @author Trisha
 */
public class manage_inventory extends javax.swing.JFrame {

    Connection conn;
    Statement stmt, stmt2;
    ResultSet result1, result2;
    int flag = 1;

    String user = "root";
    String pass = "p@ssword";
    String URL= "jdbc:mysql://localhost:3306/hajkebab";

    /** Creates new form manage_inventory */
    public manage_inventory(managementmodule managementmodule) {
        initComponents();

        viewInventory();

        this.managementmodule = managementmodule;
        managementmodule.setVisible(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        EditInventoryButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        BackButton = new javax.swing.JButton();
        AddInventoryButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        SearchButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        SearchTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        viewInventoryTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Rockwell", 0, 24));
        jLabel1.setText("Manage Inventory");

        EditInventoryButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        EditInventoryButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/editinventory.png"))); // NOI18N
        EditInventoryButton.setBorder(null);
        EditInventoryButton.setBorderPainted(false);
        EditInventoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditInventoryButtonActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel6.setText("Edit Inventory:");

        BackButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        BackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/back.png"))); // NOI18N
        BackButton.setBorder(null);
        BackButton.setBorderPainted(false);
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        AddInventoryButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        AddInventoryButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/addinventory.png"))); // NOI18N
        AddInventoryButton.setBorder(null);
        AddInventoryButton.setBorderPainted(false);
        AddInventoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddInventoryButtonActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel4.setText("Add an Inventory:");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/Haj Logo.JPG"))); // NOI18N

        SearchButton.setText("Search");
        SearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchButtonActionPerformed(evt);
            }
        });

        refreshButton.setText("Refresh Table");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        SearchTextField.setText("enter name here...");
        SearchTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchTextFieldActionPerformed(evt);
            }
        });

        viewInventoryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
            },
            new String [] {
                "InventoryID", "Name", "Quantity", "UnitofMeasure", "Price", "Used"
            }
        ));
        viewInventoryTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                viewInventoryTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(viewInventoryTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(BackButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(EditInventoryButton)
                                            .addComponent(AddInventoryButton)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel6))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(SearchButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(SearchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(refreshButton)
                                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(91, 91, 91))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AddInventoryButton)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(EditInventoryButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125, Short.MAX_VALUE)
                        .addComponent(BackButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SearchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(SearchButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(refreshButton)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 676, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void EditInventoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditInventoryButtonActionPerformed
        String selectedName = pangalan;
        System.out.println(pangalan);
        this.setVisible(false);
        edit_inventory next = new edit_inventory(this, selectedName);
        next.setVisible(true);
    }//GEN-LAST:event_EditInventoryButtonActionPerformed

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        this.setVisible(false);
        managementmodule.setVisible(true);
    }//GEN-LAST:event_BackButtonActionPerformed

    private void AddInventoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddInventoryButtonActionPerformed
        this.setVisible(false);
        add_inventory next = new add_inventory(this);
        next.setVisible(true);
    }//GEN-LAST:event_AddInventoryButtonActionPerformed

    private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchButtonActionPerformed
        DefaultTableModel tmodel;
        tmodel=(DefaultTableModel)viewInventoryTable.getModel();
        tmodel.setRowCount(0);

        try{
            Connection con = DriverManager.getConnection(URL);
            Statement stmt = (Statement) con.createStatement();

            String Name = SearchTextField.getText();

            if(Name.isEmpty()) {
                viewInventory();
                JOptionPane.showMessageDialog(this, "Input a name first", "Search failed", JOptionPane.ERROR_MESSAGE);
            }else {
                String SQL = "Select * from inventory where inventoryName='" + Name + "'";
                ResultSet rs = stmt.executeQuery(SQL);
                if(rs.next()){
                    //                while(rs.next())
                    //                {
                    System.out.println("Name: " + rs.getString("inventoryName"));
                    System.out.println("Price: " + rs.getString("price"));

                    tmodel.setRowCount(0);

                    String[] s1 = {rs.getString("inventoryID"), rs.getString("inventoryName"), rs.getString("quantity"),
                    rs.getString("UnitofMeasure"), rs.getString("price"), rs.getString("ingredientUsed")};
                    tmodel.addRow(s1);
                    //                }
                }else {
                    viewInventory();
                    JOptionPane.showMessageDialog(this, "Item does not exist", "Search failed", JOptionPane.ERROR_MESSAGE);
                }
            }
        }catch(Exception e){
            System.out.println("Error: " + e.getMessage());
        }
}//GEN-LAST:event_SearchButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        viewInventory();
}//GEN-LAST:event_refreshButtonActionPerformed

    private void SearchTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchTextFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_SearchTextFieldActionPerformed

    private void viewInventoryTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_viewInventoryTableMouseClicked
        DBConnect conn = new DBConnect();
        Connection konekta = conn.connect();
        PreparedStatement pstmt;
        ResultSet res;

        int row = viewInventoryTable.getSelectedRow();
        int column = viewInventoryTable.getSelectedColumn();
        //System.out.println(row + "," + column );
        Object value = viewInventoryTable.getValueAt(row, 1);
        pangalan = value.toString();

        /*this is to test if the pangalan that is selected on the table is found in the database*/
        //        System.out.println(pangalan);
        //
        //        try{
        //        Connection con = DriverManager.getConnection(URL);
        //        Statement stmt = (Statement) con.createStatement();
        //
        //            String SQL = "Select * from inventory where inventoryName='" + pangalan + "'";
        //            ResultSet rs = stmt.executeQuery(SQL);
        //            if(rs.next()){
        //                while(rs.next())
        //                {
        //                    System.out.println("Name: " + rs.getString("inventoryName"));
        //                    System.out.println("Price: " + rs.getString("price"));
        //
        //                String[] s1 = {rs.getString("inventoryID"), rs.getString("inventoryName"), rs.getString("quantity"),
        //                rs.getString("UnitofMeasure"), rs.getString("price"), rs.getString("ingredientUsed")};
        //                }
        //            }else
        //                {
        //                    viewInventory();
        //                    JOptionPane.showMessageDialog(this, "Item does not exist", "Search failed", JOptionPane.ERROR_MESSAGE);
        //                }
        //
        //    }catch(Exception e){
        //        System.out.println("Error: " + e.getMessage());
        //        invalidLabel1.setVisible(true);
        //    }

        //        var.setInventory_name(pangalan);
        //        man.selectedInventory(var);
    }//GEN-LAST:event_viewInventoryTableMouseClicked

        public void viewInventory(){
                /* this gets the value in the datebase to the jtable */
        DBConnect conn = new DBConnect();
        Connection konekta = conn.connect();
        PreparedStatement pstmt;
        ResultSet res;

        DefaultTableModel tmodel;
        tmodel=(DefaultTableModel)viewInventoryTable.getModel();
        tmodel.setRowCount(0);

        try{
            pstmt = konekta.prepareStatement("Select * From inventory");
            res = pstmt.executeQuery();
            while(res.next()){
                //lagyan na lang nung other stuff tulad nung in out
                //baka pwede pa ayusin to since wala pa yung form ni ate -Sam
                String[] s1 = {res.getString("inventoryID"), res.getString("inventoryName"), res.getString("quantity"),
                res.getString("UnitofMeasure"), res.getString("price"), res.getString("ingredientUsed")};
                tmodel.addRow(s1);
            }
        }catch(Exception e){
        }

        int row = viewInventoryTable.getSelectedRow();
        int column = viewInventoryTable.getSelectedColumn();
//      System.out.println(row + "," + column );
//      Object value = viewInventoryTable.getValueAt(row, column);


        conn.close();
        /*  */
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new manage_inventory(managementmodule).setVisible(true);
            }
        });
    }

    public static String pangalan;
    private static manage_inventory manage_inventory;
    private static add_inventory add_inventory;
    private static edit_inventory edit_inventory;
    private static managementmodule managementmodule;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddInventoryButton;
    private javax.swing.JButton BackButton;
    private javax.swing.JButton EditInventoryButton;
    private javax.swing.JButton SearchButton;
    private javax.swing.JTextField SearchTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton refreshButton;
    private javax.swing.JTable viewInventoryTable;
    // End of variables declaration//GEN-END:variables
}
